<?php

/**
 * This is the model class for table "{{delivery_method}}".
 *
 * The followings are the available columns in table '{{delivery_method}}':
 * @property integer $delivery_id
 * @property string $delivery_name
 * @property string $delivery_des
 * @property string $delivery_locale
 * @property double $delivery_fee
 * @property integer $delivery_weight
 * @property integer $delivery_status
 */
class DeliveryMethod extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return DeliveryMethod the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{delivery_method}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('delivery_name, delivery_des, delivery_locale, delivery_fee, delivery_weight', 'required'),
            array('delivery_weight, delivery_status', 'numerical', 'integerOnly' => true),
            array('delivery_fee', 'numerical'),
            array('delivery_name', 'length', 'max' => 100),
            array('delivery_locale', 'length', 'max' => 3),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('delivery_id, delivery_name, delivery_des, delivery_locale, delivery_fee, delivery_weight, delivery_status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'delivery_id' => 'Delivery',
            'delivery_name' => 'Delivery Name',
            'delivery_des' => 'Delivery Des',
            'delivery_locale' => 'Delivery Locale',
            'delivery_fee' => 'Delivery Fee',
            'delivery_weight' => 'Delivery Weight',
            'delivery_status' => 'Delivery Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('delivery_id', $this->delivery_id);
        $criteria->compare('delivery_name', $this->delivery_name, true);
        $criteria->compare('delivery_des', $this->delivery_des, true);
        $criteria->compare('delivery_locale', $this->delivery_locale, true);
        $criteria->compare('delivery_fee', $this->delivery_fee);
        $criteria->compare('delivery_weight', $this->delivery_weight);
        $criteria->compare('delivery_status', $this->delivery_status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function convertUserState($data) {
        $image = ($data->delivery_status == ConstantDefine::USER_STATUS_ACTIVE) ? 'active' : 'disabled';
        return bu() . '/images/' . $image . '.png';
    }

}
